﻿using System;
using System.Collections.Generic;

namespace KBEngine.Setting
{
    /// <summary>
    /// 属性版本控制  可能有些属性在旧版本中是不存在的
    /// </summary>
    [AttributeUsage(AttributeTargets.Property, Inherited = false)]
    public class CtrVer : Attribute
    {
        private Version assetVer = null;
        public readonly Version[] versions;
        public readonly Mode mode;

        public CtrVer(string ver, Mode mode = Mode.GreaterOrEqual)
        {
            this.mode = mode;
            versions = new Version[] { Version.Parse(ver) };
        }
        public CtrVer(Mode mode, params string[] verArray)
        {
            this.mode = mode;
            versions = Array.ConvertAll(verArray, Version.Parse);
        }

        public bool IsMatch()
        {
            if (assetVer == null)
                assetVer = Version.Parse("2.5.8");
            if (mode == Mode.GreaterOrEqual)
                return Array.Exists(versions, i => assetVer >= i);
            if (mode == Mode.LessOrEqual)
                return Array.Exists(versions, i => assetVer <= i);
            if (mode == Mode.Equal)
                return Array.Exists(versions, i => assetVer == i);
            return false;
        }

        public enum Mode
        {
            GreaterOrEqual,
            LessOrEqual,
            Equal,
        }
    }
}
